package com.byxy.controller;

import javax.annotation.Resource;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

import com.byxy.aspect.MyLog;
import com.byxy.entity.Agency;
import com.byxy.service.AgencyService;

@Controller
@RequestMapping(value = "/agency") // namespace="/agency"
public class AgencyController {

	@Resource
	private AgencyService agencyService;

	@MyLog(desc = "增加")
	@RequestMapping(value = "add", method = RequestMethod.POST)
	public String add(Agency agency) {
		agencyService.save(agency);
		return "redirect:/agency/list";
	}

	@MyLog(desc = "增加")
	@RequestMapping(value = "add", method = RequestMethod.GET)
	public String add() {
		return "agency/add";
	}

	@MyLog(desc = "删除")
	@RequestMapping(value = "delete", method = RequestMethod.GET)
	public String delete(int id) {
		agencyService.delete(id);
		return "redirect:/agency/list";
	}

	@MyLog(desc = "显示列表")
	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list(Model model) {
		model.addAttribute("list", agencyService.listAll());
		return "agency/list";
	}

	@MyLog(desc = "数据更新")
	@RequestMapping(value = "update", method = RequestMethod.GET)
	public String update(int id, Model model) {
		model.addAttribute("agency", agencyService.getById(id));
		return "agency/update";
	}

	@MyLog(desc = "数据更新")
	@RequestMapping(value = "update", method = RequestMethod.POST)
	public String update(Agency agency) {
		agencyService.update(agency);
		return "redirect:/agency/list";
	}
}
